package com.osdp.common.interceptor;

import com.osdp.common.constant.CommonConstant;
import com.osdp.common.util.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.mapping.MappedStatement;

import java.lang.reflect.Field;
import java.util.Date;

@Slf4j
@Intercepts(@Signature(type=Executor.class, method = "update", args = {MappedStatement.class, Object.class}))
public class SaveSqlInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("-----------intercept------------------");
        MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
        String sqlId = mappedStatement.getId();
        SqlCommandType sqlCommandType = mappedStatement.getSqlCommandType();
        Object parameter = invocation.getArgs()[1];
        if(parameter == null){
            return invocation.proceed();
        }
        System.out.println("-------------------------------sqlCommandType"+sqlCommandType);
        if(sqlCommandType == SqlCommandType.INSERT){
            Field[] fields = ConvertUtil.getAllFields(parameter);
            for(Field field : fields){
                try{
//                    UserInfo sysUser = CurrentUserInfoUtil.getUserInfo();
                    if("createBy".equals(field.getName())){
                        field.setAccessible(true);
                        Object local_createBy = field.get(parameter);
                        field.setAccessible(false);
                        if (local_createBy == null || local_createBy.equals("")) {
                            String createBy = CommonConstant.SUPER_ADMIN;
//                            (sysUser != null) {
//                                // 登录账号
//                                createBy = sysUser.getUsername();
//                            }
                            if (ConvertUtil.isNotEmpty(createBy)) {
                                field.setAccessible(true);
                                field.set(parameter, createBy);
                                field.setAccessible(false);
                            }
                        }
                    }
                    // 注入创建时间
                    if ("createTime".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_createDate = field.get(parameter);
                        field.setAccessible(false);
                        if (local_createDate == null || local_createDate.equals("")) {
                            field.setAccessible(true);
                            field.set(parameter, new Date());
                            field.setAccessible(false);
                        }
                    }
                }catch (Exception e){
                    if(log.isDebugEnabled()){
                        log.debug(e.getMessage());
                    }
                }
            }
        }
        if (SqlCommandType.UPDATE == sqlCommandType) {
            Field[] fields = null;
            if (parameter instanceof ParamMap) {
                ParamMap<?> p = (ParamMap<?>) parameter;
                parameter = p.get("param1");
                fields = ConvertUtil.getAllFields(parameter);
            } else {
                fields = ConvertUtil.getAllFields(parameter);
            }

            for (Field field : fields) {
                try {
                    if ("updateBy".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_updateBy = field.get(parameter);
                        field.setAccessible(false);
                        if (local_updateBy == null || local_updateBy.equals("")) {
                            String updateBy = "admin";
                            // 获取登录用户信息
//                            UserInfo sysUser = CurrentUserInfoUtil.getUserInfo();
//                            if (sysUser != null) {
//                                // 登录账号
//                                updateBy = sysUser.getUsername();
//                            }
                            if (ConvertUtil.isNotEmpty(updateBy)) {
                                field.setAccessible(true);
                                field.set(parameter, updateBy);
                                field.setAccessible(false);
                            }
                        }
                    }
                    if ("updateTime".equals(field.getName())) {
                        field.setAccessible(true);
                        Object local_updateDate = field.get(parameter);
                        field.setAccessible(false);
                        if (local_updateDate == null || local_updateDate.equals("")) {
                            field.setAccessible(true);
                            field.set(parameter, new Date());
                            field.setAccessible(false);
                        }
                    }
                } catch (Exception e) {
                    if(log.isDebugEnabled()){
                        log.debug(e.getMessage());
                    }
                }
            }
        }
        return invocation.proceed();
    }
    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }
}
